<?php 
namespace app\index\controller;
use think\Controller;
use think\Request;

class Personal extends Controller
{
	// 个人资料首页
	public function index($id)
	{
		$user = db('information')->where('userid',$id)->find();
		// var_dump($user);
		$this->assign('user',$user);
		$this->assign('img',$user['img']);
		return $this->fetch();
	}
	// 资料修改
	public function useredit()
	{
		if (request()->isPost()) {
			db('information')->where('userid',$_POST['userid'])->update($_POST);
			$data = db('information')->where('userid',$_POST['userid'])->find();
			$row = ['id'=>$_POST['userid'],'name'=>$data['name'],'img'=>$data['img'],'jifen'=>$data['jifen'],'type'=>$data['type']];
			session('user',$row);
			return $this->success('修改成功',url('personal/index',array('id'=>$_POST['userid'])));
		}else{
			return $this->error('想多了',url('personal/index',array('id'=>$_POST['userid'])));
		}
	}
	// 头像
	public function image($id)
	{
		$user = db('information')->where('userid',$id)->find();
		// var_dump($user);die;
		$row = ['id'=>$id,'name'=>$user['name'],'img'=>$user['img'],'jifen'=>$user['jifen'],'type'=>$user['type']];
		session('user',$row);
		$this->assign('img',$user['img']);
		return $this->fetch();
	}
	// 修改头像
	public function image_edit()
	{
		$file = request()->file('image');

		if ($file == null) {
			$this->error('未选择修改头像',url('personal/image',array('id'=>$_POST['userid'])));
			die;
		}
		$info = $file->move(ROOT_PATH . 'public/static/index' . DS .'userimg');
		if ($info) {
			$img =  ['img'=>($info->getSaveName())];
			$user = db('information')->where('userid',$_POST['userid'])->update($img);
			$this->success('修改成功',url('personal/image',array('id'=>$_POST['userid'])));
		}else{
			$this->error('修改失败',url('personal/image',array('id'=>$_POST['userid'])));
		}
		
	}
	// 积分
	public function gral($id)
	{
		$user = db('information')->where('userid',$id)->find();
		$goods = db('gral_info')->where('uid',$id)->select();
		foreach ($goods as &$val) {
			$val['pic'] = db('goods')->where('id',$val['gid'])->find()['pic'];
			$val['type'] = db('goods')->where('id',$val['gid'])->find()['type'];
		}
		// var_dump($goods);die;
		$this->assign('user',$user);
		$this->assign('goods',$goods);
		$this->assign('img',$user['img']);
		return $this->fetch();
	}
	// 兑换会员
	public function change($userid)
	{

		$user = db('information')->where('userid',$userid)->find();
		$this->assign('img',$user['img']);
		if ($user['jifen'] >= 10000) {
			$jifen = ($user['jifen'] - 10000);
			$D = ['jifen'=>$jifen,'type'=>1];
			db('information')->where('userid',$userid)->update($D);
			
			$data = db('information')->where('userid',$userid)->find();
			$row = ['id'=>$userid,'name'=>$data['name'],'img'=>$data['img'],'jifen'=>$data['jifen'],'type'=>$data['type']];
			session('user',$row);
			return '1';
		}else{
			return '0';	
		}
	}
	// 隐私
	public function privacy($id)
	{
		$user = db('information')->where('userid',$id)->find();
		$yinsi = $user['yinsi'];
		$this->assign('yinsi',$yinsi);
		$this->assign('img',$user['img']);
		return $this->fetch();
	}
	// 修改隐私
	public function edit()
	{
		if (request()->isPost()) {
			$user = input('post.');
			db('information')->where('userid',$user['userid'])->update(['yinsi'=>$user['yinsi']]);
			$this->success('修改成功',url('personal/privacy',array('id'=>$user['userid'])));
		}
	}
}